package hyperzsoft.triplea.game;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.database.DataSetObserver;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.WindowManager;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;

public class CustomSpinner extends Spinner
{

   private AlertDialog mPopup;

   /**
    * @see android.widget.Spinner#Spinner(Context)
    */
   public CustomSpinner(Context context)
   {
      super(context);
   }

   /**
    * @see android.widget.Spinner#Spinner(Context, AttributeSet)
    */
   public CustomSpinner(Context context, AttributeSet attrs)
   {
      super(context, attrs);
   }

   /**
    * @see android.widget.Spinner#Spinner(Context, AttributeSet, int)
    */
   public CustomSpinner(Context context, AttributeSet attrs, int defStyle)
   {
      super(context, attrs, defStyle);
   }

   /*
    * (non-Javadoc)
    * 
    * @see android.view.View#onAttachedToWindow()
    */
   @Override
   protected void onAttachedToWindow()
   {
      super.onAttachedToWindow();
   }

   /*
    * (non-Javadoc)
    * 
    * @see android.widget.Spinner#onDetachedFromWindow()
    */
   @Override
   protected void onDetachedFromWindow()
   {
      super.onDetachedFromWindow();

      if (mPopup != null && mPopup.isShowing())
      {
         mPopup.dismiss();
         mPopup = null;
      }
   }

   /*
    * (non-Javadoc)
    * 
    * @see android.widget.Spinner#performClick()
    */
   @Override
   public boolean performClick()
   {
      Context context = getContext();

      AlertDialog.Builder builder = new AlertDialog.Builder(context);
      CharSequence prompt = getPrompt();
      if (prompt != null)
      {
         builder.setTitle(prompt);
      }

      mPopup = builder.setSingleChoiceItems(
               new DropDownAdapter(getAdapter()),
               getSelectedItemPosition(), this).show();

      int width, height;
      width = getWidth() + getPaddingLeft() + getPaddingRight();
      height = getMeasuredHeight() * (getCount() + 1);

      WindowManager.LayoutParams WMLP = mPopup.getWindow().getAttributes();

      // width and height must be set to anything other than WRAP_CONTENT!
      WMLP.gravity = Gravity.CENTER;
      WMLP.x = 0; // x position
      WMLP.y = 0; // y position
      WMLP.height = height;
      WMLP.width = width;
      WMLP.horizontalMargin = 0;
      WMLP.verticalMargin = 0;

      mPopup.getWindow().setAttributes(WMLP);

      // ListView.getDefaultSize(size, measureSpec)
      ListView listView = mPopup.getListView();
      // listView.set
      // Remove divider between rows
      listView.setDivider(null);

      // Set custom background
      listView.setBackgroundResource(R.drawable.dropdown_dialog_background);

      // Remove background from all (grand)parent's
      ViewParent parent = listView.getParent();
      while (parent != null && parent instanceof View)
      {
         ((View) parent).setBackgroundDrawable(null);
         parent = parent.getParent();
      }

      return true;
   }

   /*
    * (non-Javadoc)
    * 
    * @see android.widget.Spinner#onClick(android.content.DialogInterface, int)
    */
   @Override
   public void onClick(DialogInterface dialog, int which)
   {
      setSelection(which);
      dialog.dismiss();
      mPopup = null;
   }

   /*
    * <p>Wrapper class for an Adapter. Transforms the embedded Adapter instance into a ListAdapter.</p>
    */
   private class DropDownAdapter implements ListAdapter, SpinnerAdapter
   {
      private final SpinnerAdapter mAdapter;

      /**
       * <p>
       * Creates a new ListAddapter wrapper for the specified adapter.
       * </p>
       * 
       * @param adapter the Adapter to transform into a ListAdapter
       */
      public DropDownAdapter(SpinnerAdapter adapter)
      {
         this.mAdapter = adapter;
      }

      /*
       * (non-Javadoc)
       * 
       * @see android.widget.Adapter#getCount()
       */
      @Override
      public int getCount()
      {
         return mAdapter == null ? 0 : mAdapter.getCount();
      }

      /*
       * (non-Javadoc)
       * 
       * @see android.widget.Adapter#getItem(int)
       */
      @Override
      public Object getItem(int position)
      {
         return mAdapter == null ? null : mAdapter.getItem(position);
      }

      /*
       * (non-Javadoc)
       * 
       * @see android.widget.Adapter#getItemId(int)
       */
      @Override
      public long getItemId(int position)
      {
         return mAdapter == null ? -1 : mAdapter.getItemId(position);
      }

      /*
       * (non-Javadoc)
       * 
       * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
       */
      @Override
      public View getView(int position, View convertView, ViewGroup parent)
      {
         return getDropDownView(position, convertView, parent);
      }

      /*
       * (non-Javadoc)
       * 
       * @see android.widget.SpinnerAdapter#getDropDownView(int, android.view.View, android.view.ViewGroup)
       */
      @Override
      public View getDropDownView(int position, View convertView, ViewGroup parent)
      {
         return mAdapter == null ? null :
            mAdapter.getDropDownView(position, convertView, parent);
      }

      /*
       * (non-Javadoc)
       * 
       * @see android.widget.Adapter#hasStableIds()
       */
      @Override
      public boolean hasStableIds()
      {
         return mAdapter != null && mAdapter.hasStableIds();
      }

      /*
       * (non-Javadoc)
       * 
       * @see android.widget.Adapter#registerDataSetObserver(android.database.DataSetObserver)
       */
      @Override
      public void registerDataSetObserver(DataSetObserver observer)
      {
         if (mAdapter != null)
         {
            mAdapter.registerDataSetObserver(observer);
         }
      }

      /*
       * (non-Javadoc)
       * 
       * @see android.widget.Adapter#unregisterDataSetObserver(android.database.DataSetObserver)
       */
      @Override
      public void unregisterDataSetObserver(DataSetObserver observer)
      {
         if (mAdapter != null)
         {
            mAdapter.unregisterDataSetObserver(observer);
         }
      }

      @Override
      public boolean areAllItemsEnabled()
      {
         return true;
      }

      /*
       * (non-Javadoc)
       * 
       * @see android.widget.ListAdapter#isEnabled(int)
       */
      @Override
      public boolean isEnabled(int position)
      {
         return true;
      }

      /*
       * (non-Javadoc)
       * 
       * @see android.widget.Adapter#isEmpty()
       */
      @Override
      public boolean isEmpty()
      {
         return getCount() == 0;
      }

      /*
       * @return overridden to return 1
       * 
       * @see android.widget.Adapter#getItemViewType(int)
       */
      @Override
      public int getItemViewType(int position)
      {
         return 0;
      }

      /*
       * (non-Javadoc)
       * 
       * @return overridden to return 1
       * 
       * @see android.widget.Adapter#getViewTypeCount()
       */
      @Override
      public int getViewTypeCount()
      {
         return 1;
      }

   }
}